﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   Elevator. A platform that moves up-down
// Student Number:     S10096666C
// Student Name:       Trinh Quang Anh

//////////////////////////////////////////////////////////////////////////

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game.Platforms
{
    internal class Elevator : IPlatform
    {
        private readonly float _travelDistance;
        private int _height;
        private float _maxY;
        private float _minY;
        private Vector2 _position;
        private float _speed; // pixels per second
        private Texture2D _texture;
        private PlatformType _type;
        private int _width;

        private readonly Level _level;

        public Elevator(Level level, float travelDistance, float speed)
        {
            _level = level;
            //_travelDistance = travelDistance;
            _travelDistance = 160; // force
            _speed = speed;
        }

        #region IPlatform Members

        public void Spawn(PlatformType type, Texture2D texture, int x, int y)
        {
            _type = type;
            _texture = texture;
            _width = texture.Width;
            _height = texture.Height;
            _position = new Vector2(x * Game1.GridSize, y * Game1.GridSize);

            _minY = _position.Y - _travelDistance;
            _maxY = _position.Y;
        }

        public void Spawn(PlatformType type, Texture2D texture, Vector2 pos)
        {
            _type = type;
            _texture = texture;
            _width = texture.Width;
            _height = texture.Height;
            _position = pos;

            _minY = _position.Y - _travelDistance;
            _maxY = _position.Y;
        }

        // move up: pos.Y - speed
        // move down: pos.y - (-speed)
        public void Update(GameTime gameTime)
        {
            // reached limit
            if (_position.Y < _minY || _position.Y > _maxY)
            {
                _speed = -_speed;
            }

            _position.Y -= _speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            var bounds = BoundingRect;

            foreach (var platform in _level.Platforms)
            {
                if (platform == this) return;

                Vector2 depth = bounds.GetIntersectionDepth(platform.BoundingRect);
                float absDepthY = Math.Abs(depth.Y);

                if (absDepthY > 0)
                {
                    Console.WriteLine("Elevator Collide");
                    _position.Y = _position.Y + depth.Y;
                    _speed = -_speed;
                }

            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, _position, Color.White);
        }

        public Vector2 Position
        {
            get { return _position; }
        }

        public PlatformType Type
        {
            get { return _type; }
        }

        public Rectangle BoundingRect
        {
            get
            {
                return new Rectangle((int)_position.X, (int)_position.Y, _width, _height);
            }
        }

        #endregion
    }
}